package com.yolo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yolo.common.annotation.Log;
import com.yolo.common.enums.BusinessType;
import com.yolo.entity.Resource;
import com.yolo.entity.dto.RoleTree;
import com.yolo.entity.dto.TreeResource;
import com.yolo.service.ResourceService;
import com.yolo.util.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/resource")
@CrossOrigin
public class ResourceController {
    @Autowired
    private ResourceService resourceService;

    @Log(title = "资源模块", businessType = BusinessType.QUERY)
    @GetMapping("")
    public Result resourceData() {
        List<TreeResource> treeResources = new ArrayList<>();
        List<Resource> re1 = resourceService.list(new QueryWrapper<Resource>().isNull("f_id"));
        for (Resource r1 : re1) {
            TreeResource t1 = new TreeResource();
            BeanUtils.copyProperties(r1, t1);
            List<Resource> re2 = resourceService.list(new QueryWrapper<Resource>().eq("f_id", r1.getResourceId()));
            List<TreeResource> tr1 = new ArrayList<>();
            for (Resource r2 : re2) {
                TreeResource t2 = new TreeResource();
                BeanUtils.copyProperties(r2, t2);
                tr1.add(t2);
                List<Resource> re3 = resourceService.list(new QueryWrapper<Resource>().eq("f_id", r2.getResourceId()));
                List<TreeResource> tr2 = new ArrayList<>();
                for (Resource r3 : re3) {
                    TreeResource t3 = new TreeResource();
                    BeanUtils.copyProperties(r3, t3);
                    tr2.add(t3);
                }
                if (tr2.size() != 0) {
                    t2.setChildren(tr2);
                }
            }
            t1.setChildren(tr1);
            treeResources.add(t1);
        }
        return Result.succ(treeResources);
    }

    @Log(title = "资源模块", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public Result exitResource(String dialogResourceName, String dialogUrl, int dialogResourceId) {
        Resource resource = new Resource();
        resource.setResourceName(dialogResourceName);
        resource.setUrl(dialogUrl);
        resource.setResourceId(dialogResourceId);
        resourceService.updateById(resource);
        return Result.succ(null);
    }

    @GetMapping("/treeResourceData")
    public Result treeResource() {
        List<RoleTree> j1 = new ArrayList<>();
        List<Resource> p1 = resourceService.list(new QueryWrapper<Resource>().isNull("f_id"));//使用条件构造器判断f_id是否为空
        for (Resource r1 : p1) {
            RoleTree rt1 = new RoleTree();
            rt1.setId(r1.getResourceId());
            rt1.setLabel(r1.getResourceName());
            List<Resource> p2 = resourceService.list(new QueryWrapper<Resource>().eq("f_id", r1.getResourceId()));//利用条件构造器判断我的父资源id是否和我的资源id相等
            List<RoleTree> j2 = new ArrayList<>();
            for (Resource r2 : p2) {
                RoleTree rt2 = new RoleTree();
                rt2.setId(r2.getResourceId());
                rt2.setLabel(r2.getResourceName());
                j2.add(rt2);
                List<Resource> p3 = resourceService.list(new QueryWrapper<Resource>().eq("f_id", r2.getResourceId()));
                List<RoleTree> j3 = new ArrayList<>();//节点
                for (Resource r3 : p3) {
                    RoleTree rt3 = new RoleTree();
                    rt3.setId(r3.getResourceId());
                    rt3.setLabel(r3.getResourceName());
                    j3.add(rt3);
                }
                rt2.setChildren(j3);
            }
            rt1.setChildren(j2);
            j1.add(rt1);
        }
        return Result.succ(j1);
    }
}
